package com.cskaoyan.gateway.controller.shopping;

import com.cskaoyan.gateway.query.shopping.GoodsQuery;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @date: 2022-01-20-09-23
 * @author: ZYY
 * @description: 商品的Controller
 */

@RestController
@RequestMapping("shopping")
public class ProductController {

    @Reference(timeout = 10000,check = false)
    IProductService productService;

    /**
     * @author:zyy
     * @description:  根据商品id查看商品详情
     * @date:2022/01/20 10:01
     * @param productId  商品id
     * @return com.mall.commons.result.ResponseData
     */
    @GetMapping("product/{id}")
    @Anoymous
    public ResponseData ViewProductDetail(@PathVariable("id") Long productId) {
        // 将参数封装到bo中
        ProductDetailRequest productDetailRequest = new ProductDetailRequest();
        productDetailRequest.setId(productId);
        // 将bo传输到服务层 获得最终结果
         ProductDetailResponse productResponse= productService.getProductDetail(productDetailRequest);

         // 判断返回的中数据有没有异常
        if (ShoppingRetCode.SUCCESS.getCode().equals(productResponse.getCode())){
            // 没有异常 将返回的对象封装到vo中
            return new ResponseUtil().setData(productResponse.getProductDetailDto());
        }
        // 有错误或者异常
        return new ResponseUtil().setErrorMsg(productResponse.getMsg());
    }

    /**
     * @author:zyy
     * @description: 查询商品列表
     * @date:2022/01/20 17:04
     * @param goodsQuery
     * @return com.mall.commons.result.ResponseData
     */
    @GetMapping("goods")
    @Anoymous
    public ResponseData PagingQueryGoodsList(GoodsQuery goodsQuery){
        // 将参数封装到bo中
        AllProductRequest request = new AllProductRequest();
        request.setCid(goodsQuery.getCid());
        request.setPage(goodsQuery.getPage());
        request.setPriceGt(goodsQuery.getPriceGt());
        request.setPriceLte(goodsQuery.getPriceLte());
        request.setSize(goodsQuery.getSize());
        request.setSort(goodsQuery.getSort());

        // 将bo传输到服务提供者的Service层
        AllProductResponse response = productService.getAllProduct(request);

        // 判断查询是否成功
        if(ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())){
            AllProductResponse2 response2 = new AllProductResponse2();
            response2.setData(response.getProductDtoList());
            response2.setTotal(response.getTotal());
            return new ResponseUtil().setData(response2);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * @author:zyy
     * @description: 查询推荐的产品
     * @date:2022/01/20 20:13
     * @return com.mall.commons.result.ResponseData
     */
    @GetMapping("recommend")
    @Anoymous
    public ResponseData queryRecommendGoods(){
        // 无参数不用封装
        // 调用服务提供者的Service层
        RecommendResponse response = productService.getRecommendGoods();

        // 判断是否成功
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
//            return new ResponseUtil().setData(response.getPanelContentItemDtos());
            return new ResponseUtil().setData(response.getPanelContentItemDtos());
        }
        // 不成功返回错误信息
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
}